﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_HeThong_LienKetWeb_Info : System.Web.UI.UserControl
{
    TblLienKetWebManager _TblLienKetWebManager = new TblLienKetWebManager();
    public int CateID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { CateID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { CateID = 0; }

            if (CateID == 0) //Add cate
            {
                lblTitle.Text = "Thêm liên  kết";
                Toolbox1.ShowBack = Toolbox1.ShowRefesh = Toolbox1.ShowDel = false;
            }
            else //sua cate
            {
                lblTitle.Text = "Sửa liên  kết: " + CateID;
                BindData(CateID);
            }
        }
    }

    protected void BindData(int ID)
    {
        TblLienKetWebEntity _TblLienKetWebEntity = _TblLienKetWebManager.SelectOne(ID);
        if (_TblLienKetWebEntity == null) { lblthongbao.Text = "<img src='../../Images/validatebox_warning.png' alt='Cảnh báo' width='16' height='16' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        hidID.Value = ID.ToString();
        txtID.Text = ID.ToString();
        txtName.Text = Server.HtmlDecode(_TblLienKetWebEntity.TenLienKet);
        txtMota.Text = Server.HtmlDecode(_TblLienKetWebEntity.Mota);
        txtUrl.Text = Server.HtmlDecode(_TblLienKetWebEntity.Url);
        chkStatus.Checked = Convert.ToBoolean(_TblLienKetWebEntity.TrangThai);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblLienKetWebManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/LienKetWebEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        if (txtName.Text == "") { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Nhập tên liên kết !</span>"; txtName.Focus(); return; }
        TblLienKetWebEntity _TblLienKetWebEntity = new TblLienKetWebEntity();
        if (!string.IsNullOrEmpty(hidID.Value)) _TblLienKetWebEntity.Id = Convert.ToInt32(hidID.Value);//update

        _TblLienKetWebEntity.TenLienKet = Server.HtmlEncode(txtName.Text);
        _TblLienKetWebEntity.Url = Server.HtmlEncode(txtUrl.Text);
        _TblLienKetWebEntity.Mota = Server.HtmlEncode(txtMota.Text);
        _TblLienKetWebEntity.TrangThai = (chkStatus.Checked == true) ? true : false;

        if (!string.IsNullOrEmpty(hidID.Value))
            _TblLienKetWebManager.Update(_TblLienKetWebEntity);//update
        else
        {
            _TblLienKetWebManager.Insert(_TblLienKetWebEntity);//Insert
            hidID.Value = _TblLienKetWebEntity.Id.ToString();
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
}